<template>
  <div class="login_container">
    <el-row>
      <!-- 左侧占位符 -->
      <el-col :span="14" :xs="0"></el-col>
      <el-col :span="10" :xs="24">
        <el-form
          :model="form"
          class="login_form"
          :rules="rules"
          ref="loginFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到秋秋钟汉良甄选</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="form.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              v-model="form.password"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_button"
              type="primary"
              size="default"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElNotification } from "element-plus";
// 引入用户相关的小仓库
import useUserStore from "../../store/modules/user";
import { getTime } from "../../utils/time";

let useStore = useUserStore();
let router = useRouter();
let route = useRoute();

const form = reactive({
  username: "",
  password: "",
});
// 定义变量控制登录点击后加载的效果
const loading = ref(false);
const loginFormRef = ref();

// 配置表单检验需要的规则
const rules = {
  // trigger 规则触发的时间，change：文本改变的时候触发，blur失去焦点的时候触发校验规则
  username: [
    {
      required: true,
      min: 5,
      max: 15,
      message: "用户名长度为5-15个字符",
      trigger: "change",
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 20,
      message: "密码长度为6-20个字符",
      trigger: "change",
    },
  ],
};
// 登录
const login = async () => {
  //点击登录按钮以后干什么?  通知仓库发登录请求  请求成功->首页展示数据的地方  请求失败->弹出登录失败信息
  // 校驗表單：所有的表单项校验通过在发送请求
  await loginFormRef.value.validate();
  // 开始加载 加载效果
  loading.value = true;
  try {
    //保证登录成功
    await useStore.userLoin(form);
    // 提示登录成功
    ElNotification({
      title: `Hi，${getTime()}好`,
      message: "欢迎回来!",
      type: "success",
      duration: 2000,
    });
    // 停止加載效果
    loading.value = false;
    //编程式导航跳转到展示数据首页;判断登录的时候,路由路径当中是否有query参数，如果有就往query参数跳转，没有跳转到首页
    let redirect: string = route.query.redirect as string;
    router.push({ path: redirect || "/" });
  } catch (error) {
    //弹出错误信息
    ElNotification({
      message: (error as Error).message,
      type: "error",
      duration: 2000,
    });
    // 停止加載效果
    loading.value = false;
  }
};
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    width: 80%;
    position: relative;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    top: 30vh;
    padding: 40px;
    box-sizing: border-box;

    h1 {
      color: #fff;
      font-size: 40px;
    }
    h2 {
      margin: 20px 0 !important;
      color: #fff;
      font-size: 20px;
    }

    .login_button {
      width: 100%;
    }
  }
}
</style>
